<template>
    <div class="app-container">
        <el-form :inline="true"  class="demo-form-inline" style="margin-top: 2%">
            <el-row >
                <el-col >
                    <el-form-item  size="mini" label="创建时间">
                        <el-date-picker
                                v-model="searchParams.time"
                                type="daterange"
                                value-format="yyyy-MM-dd"
                                range-separator="-"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>

                    </el-form-item>

                    <el-form-item size="mini" label="表名称">
                        <el-input size="mini" v-model="searchParams.tableName" placeholder="表名称"></el-input>
                    </el-form-item>
                    <el-form-item size="mini" label="注释">
                        <el-input size="mini" v-model="searchParams.tableComment" placeholder="注释"></el-input>
                    </el-form-item>
                    <el-button  type="primary" @click="onSubmit" size="small" icon="el-icon-search"  v-hasPermi="['tool:code:search']">搜索</el-button>
                </el-col>


            </el-row>
        </el-form>

        <template>
            <el-table
                    :data="loglist"
                    v-loading="loading"
                    element-loading-text="拼命加载中"
                    border
                    style="width: 100%">
                <el-table-column
                        type="index"
                        width="50">
                </el-table-column>
                <el-table-column
                        label="表名称"
                        prop="tableName"
                        width="330">
                </el-table-column>
                <el-table-column
                        label="引擎"
                        prop="ENGINE"
                        width="330">
                </el-table-column>
                <el-table-column
                        label="创建时间"
                        prop="createTime"
                        :formatter="dateFormat"
                        width="330">
                </el-table-column>
                <el-table-column
                        label="注释"
                        prop="tableComment"
                        width="330">
                </el-table-column>
                <el-table-column label="操作"  width="300">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="success"
                                icon="el-icon-download"
                                v-hasPermi="['tool:code:gen']"
                                @click="generate(scope.row)">生成代码</el-button>

                    </template>
                </el-table-column>
            </el-table>
        </template>

        <div class="page">
          <Pagination v-show="total>0"
                      :total="total"
                      :page.sync="searchParams.pageNum"
                      :limit.sync="searchParams.pageSize"
                      @pagination="showAllTables" />
        </div>

    </div>
</template>

<script>
    import {generateCode, showAllTables} from '@/api/generate/generate'
    import Pagination from '@/components/Pagination'
    export default {
        name: "index",
        inject: ['reload'],
        components:{
          Pagination
        },
        data() {
            return {
                total: 0,
                searchParams:{
                    time:null,
                    pageNum: 1,
                    pageSize: 10,
                    createBeginTime:'',
                    createEdnTime:'',
                    tableName:'',
                    tableComment:'',
                    tables:[]
                },
                loglist:[],
                currentPage: 1,
                dialogVisible: false,
                log:{},
                content:'',
                // 遮罩层
                loading: true,
            }
        },
        created(){
            this.showAllTables();
        },
        methods: {
          showAllTables() {
            this.loading = true;
            showAllTables(this.searchParams).then(res=>{
              this.loglist = res.data.result;
              this.total = parseInt(res.data.total);
              this.loading = false;
            })
          },
            onSubmit() {
                if (this.searchParams.time!=null) {
                    this.searchParams.createBeginTime = this.searchParams.time[0];
                    this.searchParams.createEdnTime = this.searchParams.time[1];
                }else {
                    this.searchParams.createBeginTime = null;
                    this.searchParams.createEdnTime = null;
                }

                showAllTables(this.searchParams).then(res=>{
                    this.loglist = res.data.result;
                    this.total = parseInt(res.data.total);

                })
            },
            handleClose(done) {
                this.$confirm('确认关闭？')
                    .then(_ => {
                        done();
                    })
                    .catch(_ => {});
            },

            generate(row) {
              this.searchParams.tables=[];
                this.searchParams.tables.push(row.tableName);
                generateCode(this.searchParams.tables).then(res=>{
                    let blob = new Blob([res], {type: 'application/zip'});
                    window.location.href = window.URL.createObjectURL(blob);
                })
            },
          dateFormat(row){

            let datetime = row.createTime;
            if(datetime){
              datetime = new Date(datetime);
              let y = datetime.getFullYear() + '-';
              let mon = datetime.getMonth()+1 + '-';
              let d = datetime.getDate();
              return y + mon + d;
            }
            return ''

          },

        }
    }
</script>

<style lang="scss" scoped>
    .el-button--small, .el-button--small.is-round{
        margin-left: 10px;
    }
    .page{
        float: right;
    }
    pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
</style>
